using System;
using System.Linq;
using System.Web.Mvc;
using C3.Directory.Services;
using StructureMap;

namespace CSI.Directory.Web.Filters
{
    public class RestrictedAction : ActionFilterAttribute
    {


        public ISessionService SessionService
        { 
            get { return ObjectFactory.GetInstance<ISessionService>(); }
            
        }

        private readonly string[] role;
        private readonly string loginUrl;
        public RestrictedAction(){}
        public RestrictedAction(string[] r, string login)
        {
            role = r;
            loginUrl = login;
        }

        
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            if (SessionService.CurrentUser != null && role.Any(r => SessionService.CurrentUser.IsInRole(r)))
            {
                return;
            }
            //If this is an unathorized Json request add it to the response
            if(filterContext.HttpContext.Request.AcceptTypes.Where(t => t == "application/json").ToList().Count > 0)
            {
                var result  = new JsonResult(){};
                result.Data = new {Successful = false, Exception = "401"};
                filterContext.Result = result;
                return;
            }
            //HttpUnauthorizedResult - need to use here
            filterContext.Result = string.IsNullOrEmpty(loginUrl) 
                ? new RedirectResult("/Login") : new RedirectResult(loginUrl);
            filterContext.HttpContext.Response.StatusCode = 401;
            filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
            
            
            
            
        }
    }

    public class AccountAction : ActionFilterAttribute
    {


        public ISessionService SessionService
        {
            get { return ObjectFactory.GetInstance<ISessionService>(); }

        }

        private readonly string[] role;
        
        public AccountAction() { }
        public AccountAction(string[] r)
        {
            role = r;
           
        }


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var id = Int32.Parse(filterContext.RouteData.Values["id"].ToString());
            if (SessionService.CurrentUser != null &&
                SessionService.CurrentUser.Id == id &&
                role.Any(r => SessionService.CurrentUser.IsInRole(r)))
            {
                return;
            }
            //If this is an unathorized Json request add it to the response
            if (filterContext.HttpContext.Request.AcceptTypes.Where(t => t == "application/json").ToList().Count > 0)
            {
                var result = new JsonResult() { };
                result.Data = new { Successful = false, Exception = "401" };
                filterContext.Result = result;
                return;
            }
            
            filterContext.Result = new RedirectResult("/Login");
            filterContext.HttpContext.Response.StatusCode = 401;
            filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
            



        }
    }
}